package com.lrkj.web.controller.system;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.constant.UserConstants;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.system.domain.Category;
import com.lrkj.system.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类字典Controller
 *
 * @author yajun_ren
 * @date 2021-03-24
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询分类字典列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public AjaxResult list(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        return AjaxResult.success(list);
    }

    /**
     * 导出分类字典列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "分类字典", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 获取分类字典详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") String categoryId) {
        return AjaxResult.success(categoryService.selectCategoryById(categoryId));
    }

    /**
     * 新增分类字典
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "分类字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        if (UserConstants.NOT_UNIQUE.equals(categoryService.checkCodeUnique(category))) {
            return AjaxResult.error("分类代码'" + category.getCategoryCode() + "'已存在");
        }
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类字典
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "分类字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        if (UserConstants.NOT_UNIQUE.equals(categoryService.checkCodeUnique(category))) {
            return AjaxResult.error("分类代码'" + category.getCategoryCode() + "'已存在");
        }
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类字典
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "分类字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable String[] categoryIds) {
        return toAjax(categoryService.deleteCategoryByIds(categoryIds));
    }


    @GetMapping("/treeData/{categoryId}")
    public AjaxResult treeData(Category category, @PathVariable String categoryId) {
        List<Category> list = categoryService.treeData(category, categoryId);
        return AjaxResult.success(list);
    }

}
